<script setup lang="js">
import { ref,onMounted } from "vue";
import {Tickets} from "@element-plus/icons-vue";
import {fetchSelectList} from "@/api/flashSession.js";
import {useRoute, useRouter} from "vue-router";

const route = useRoute();
const router = useRouter();

const selectTimeList = ref([]);
onMounted(() => {
  getSelectTimeList();
});

// 获取秒杀列表
const getSelectTimeList = () => {
  fetchSelectList({flashPromotionId: route.query.id}).then(res => {
    selectTimeList.value = res.data.data;
  });
}

// 显示关联商品列表
const handleShowRelation = (index, row) => {
  router.push({path: '/flash/productRelationList', query: {
      flashPromotionId: route.query.id,
      flashPromotionSessionId:row.id
    }})
}

// 格式化时间
const formatTime = (time) => {
  if (!(time instanceof Date)) {
    time = new Date(time);
  }
  const hour = String(time.getHours()).padStart(2, '0');
  const minute = String(time.getMinutes()).padStart(2, '0');
  const second = String(time.getSeconds()).padStart(2, '0');
  return `${hour}:${minute}:${second}`;
}

</script>

<template>
  <div class="select-time">
    <!--时间段列表-->
    <div class="add-list">
      <div class="add-btn">
        <el-icon>
          <Tickets/>
        </el-icon>
        <span>数据列表</span>
      </div>
    </div>

    <!--数据列表-->
    <div class="data-list">
      <el-table ref="flashSessionTable"
                :data="selectTimeList"
                style="width: 100%;" border>
        <el-table-column label="编号" width="100" align="center">
          <template  #default="{row}">{{row.id}}</template>
        </el-table-column>
        <el-table-column label="秒杀时间段名称" align="center">
          <template  #default="{row}">{{row.name}}</template>
        </el-table-column>
        <el-table-column label="每日开始时间" align="center">
          <template  #default="{row}">{{ formatTime(row.startTime) }}</template>
        </el-table-column>
        <el-table-column label="每日结束时间" align="center">
          <template  #default="{row}">{{ formatTime(row.endTime) }}</template>
        </el-table-column>
        <el-table-column label="商品数量" align="center">
          <template #default="{row}">{{row.productCount}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="{row}">
            <el-button type="text"
                       @click="handleShowRelation(index, row)">商品列表
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>
  </div>
</template>

<style scoped lang="scss">

.select-time {
  margin-top: 20px;
  display: flex;
  flex-direction: column;


  .add-list {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 15px 20px;
    margin-bottom: 20px;
    border: 1px solid #d5d5d5;
    border-radius: 5px;

    .add-btn {
      display: flex;
      align-items: center;

      span {
        margin-left: 8px;
        font-size: 18px;
      }
    }
  }
}

</style>